package com.example.forum.services.impl;


import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.dao.UserMapper;
import com.example.forum.exception.ApplicationException;
import com.example.forum.model.User;
import com.example.forum.services.IUserService;
import com.example.forum.utils.MD5Util;
import com.example.forum.utils.StringUtil;
import com.example.forum.utils.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Slf4j
@Service
public class UserServiceImpl implements IUserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public void createNormalUser(User user) {
        //1.非空校验
        if (user == null || StringUtil.isEmpty(user.getUsername())
        || StringUtil.isEmpty(user.getNickname()) || StringUtil.isEmpty(user.getPassword())) {
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常，统一抛出ApplicationException
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
      //2.按用户名查询用户信息
        User existsUser = userMapper.selectByUserName(user.getUsername());
        //2.1 判断用户是否存在
        if (existsUser != null) {
            log.info(ResultCode.FAILED_USER_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_EXISTS ));
        }
        // 3.新增用户，设置默认值
        user.setGender((byte) 2);
        user.setArticleCount(0);
        user.setIsAdmin((byte) 0);
        user.setState((byte) 0);
        user.setDeleteState((byte) 0);
        //当前日期
        Date date = new Date();
        user.setCreateTime(date);
        user.setUpdateTime(date);

        //4. 写入数据库
        int row = userMapper.insertSelective(user);
        if (row != 1) {
            log.info(ResultCode.FAILED_CREATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }
        //新增成功
        log.info("新增用户成功  username = " + user.getUsername() + " . ");
    }

    @Override
    public User selectByUserName(String username) {
        //非空校验
        if (StringUtil.isEmpty(username)) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        return userMapper.selectByUserName(username);
    }

    @Override
    public User login(String username, String password) {
        //非空校验
        if (StringUtil.isEmpty(username) || StringUtil.isEmpty((password))) {
            log.warn(ResultCode.FAILED_LOGIN.toString() + ", username = " + username);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_LOGIN));
        }
        //根据用户名查询用户信息
        User user = userMapper.selectByUserName(username);
        //对查询结果进行非空校验
        if (user == null) {
            log.warn(ResultCode.FAILED_LOGIN.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_LOGIN));
        }
        //对密码做校验
        String encryptPassword = MD5Util.md5salt(password,user.getSalt());

        //将密码和数据库中的密码做比对
        if (!encryptPassword.equalsIgnoreCase(user.getPassword())) {
            log.warn(ResultCode.FAILED_LOGIN.toString() + ",密码错误， username = " +username);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_LOGIN));
        }

        return user;
    }

    @Override
    public User selectById(Long id) {
        //非空校验
        if (id == null) {
            log.warn(ResultCode.FAILED_LOGIN.toString() + ", id = " + id);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_LOGIN));
        }

        User user = userMapper.selectByPrimaryKey(id);
        return user;

    }

    @Override
    public void addOneArticleCountById(Long id) {
        if (id == null || id == 0) {
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        User user = userMapper.selectByPrimaryKey(id);
        if (user == null) {
            log.warn(ResultCode.ERROR_IS_NULL .toString() + ",user id " + id);
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL ));
        }
       User updateUser = new User();
        updateUser.setId(user.getId());
        updateUser.setArticleCount(user.getArticleCount()+1);
        //调用DAO执行更新
        int row = userMapper.updateByPrimaryKeySelective(updateUser);
        //判断受影响的行数
        if (row != 1) {
            log.warn(ResultCode.FAILED.toString() + "受影响的行数不等于1");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }

    }

    @Override
    public void subOneArticleCountById(Long id) {
        if (id == null || id == 0) {
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        User user = userMapper.selectByPrimaryKey(id);
        if (user == null) {
            log.warn(ResultCode.ERROR_IS_NULL .toString() + ",user id " + id);
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL ));
        }
        User updateUser = new User();
        updateUser.setId(user.getId());
        updateUser.setArticleCount(user.getArticleCount()-1);
        if (updateUser.getArticleCount() < 0) {
            updateUser.setArticleCount(0);
        }
        //调用DAO执行更新
        int row = userMapper.updateByPrimaryKeySelective(updateUser);
        //判断受影响的行数
        if (row != 1) {
            log.warn(ResultCode.FAILED.toString() + "受影响的行数不等于1");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }

    @Override
    public void modifyInfo(User user) {
         //1.非空校验
        if (user == null || user.getId() == null || user.getId() <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //2.校验用户是否存在
        User existsUser = userMapper.selectByPrimaryKey(user.getId());
        if (existsUser == null) {
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }
        //3.定义一个标志位
        boolean checkAttr = false; // false表示没有校验通过

        //4.定义一个更新对象，防止传入的user中设置了多余的属性
        //导致当使用动态sql进行更新的时候，覆盖了没有进行校验的属性
        User updateUser = new User();
        //5.设置用户id
        updateUser.setId(user.getId());
        //6.对每一个参数进行校验并赋值
         //校验用户名
        if (!StringUtil.isEmpty(user.getUsername())
        && !user.getUsername().equals(existsUser.getUsername())) {
            //通过用户名去数据库中查询是否存在该用户
            User checkUser = userMapper.selectByUserName(user.getUsername());
            if (checkUser != null) {
                log.warn(ResultCode.FAILED_USER_EXISTS.toString());
                throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_EXISTS));
            }
            //数据库中没有该用户名，可以进行修改
            updateUser.setUsername(user.getUsername());
            //更新标志位
            checkAttr = true;
        }
        //校验昵称
        if (!StringUtil.isEmpty(user.getNickname())
                && !user.getNickname().equals(existsUser.getNickname())) {
            //更新昵称
            updateUser.setNickname(user.getNickname());
            //更新标志位
            checkAttr = true;
        }
        //校验性别
        if (user.getGender() != null && user.getGender()!= existsUser.getGender()) {
            //设置性别
            updateUser.setGender(user.getGender());
            //合法性校验
            if (updateUser.getGender() > 2 || updateUser.getGender() < 0) {
                updateUser.setGender((byte) 2);
            }
            //更新标志位
            checkAttr = true;
        }
        //校验Email
        if (!StringUtil.isEmpty(user.getEmail())
                && !user.getEmail().equals(existsUser.getEmail())) {
            //更新邮箱
            updateUser.setEmail(user.getEmail());
            //更新标志位
            checkAttr = true;
        }
        //校验电话号码
        if (!StringUtil.isEmpty(user.getPhoneNum())
                && !user.getPhoneNum().equals(existsUser.getPhoneNum())) {
            //更新电话号码
            updateUser.setPhoneNum(user.getPhoneNum());
            //更新标志位
            checkAttr = true;
        }
        //校验个人简介
        if (!StringUtil.isEmpty(user.getRemark())
                && !user.getRemark().equals(existsUser.getRemark())) {
            // 更新个人简介
            updateUser.setRemark(user.getRemark());
            //更新标志位
            checkAttr = true;
        }

        //7.判断标志位来决定是否需要更新
        if (checkAttr == false) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //8.调用DAO
        int row = userMapper.updateByPrimaryKeySelective(updateUser);
        //判断受影响的行数
        if (row != 1) {
            log.warn(ResultCode.FAILED.toString() + "受影响的行数不等于1");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }

    }

    @Override
    public void modifyPassword(Long id, String newPassword, String oldPassword) {
          //非空校验
        if (id == null || StringUtil.isEmpty(newPassword) || StringUtil.isEmpty(oldPassword)) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //获取对象
        User user = userMapper.selectByPrimaryKey(id);
        //判断用户是否存在
        if (user == null || user.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }
        //校验老密码是否正确
        String oldEncryptPassword = MD5Util.md5salt(oldPassword,user.getSalt());
        if (!oldEncryptPassword.equalsIgnoreCase(user.getPassword())) {
            log.warn(ResultCode.FAILED.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
        //新盐
        String salt = UUIDUtil.UUID_32();
        //新密码的密文
        String encryptPassword = MD5Util.md5salt(newPassword,salt);
        //设置到更新的对象中
        User updateUser = new User();
        updateUser.setId(id);
        updateUser.setPassword(encryptPassword);
        updateUser.setSalt(salt);
        Date date = new Date();
        updateUser.setUpdateTime(date);  //更新时间
         //调用DAO
        int row = userMapper.updateByPrimaryKeySelective(updateUser);
        if (row != 1) {
            log.warn(ResultCode.FAILED.toString() + "受影响的行数不等于1");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }

    @Override
    public void updateByPrimaryKeySelective(User user) {
        //1.非空校验
        if (user == null || user.getId() == null || user.getId() <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //2.校验用户是否存在
        User existsUser = userMapper.selectByPrimaryKey(user.getId());
        if (existsUser == null) {
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }
        //3.构造要更新的对象
        User updateUser = new User();
        updateUser.setId(user.getId());
        if (!StringUtil.isEmpty(user.getAvatarUrl())
                && !user.getAvatarUrl().equals(existsUser.getAvatarUrl())) {
            // 更新个人头像
            updateUser.setAvatarUrl(user.getAvatarUrl());
        }
        //调用DAO
        int row = userMapper.updateByPrimaryKeySelective(updateUser);
        //判断受影响的行数
        if (row != 1) {
            log.warn(ResultCode.FAILED.toString() + "受影响的行数不等于1");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }

    }
}
